$(function() {
    $(".mobile").html(localStorage.userMobile);
    //获取验证码
    var token = localStorage.token;
    var flag = false;
    $(".code").on("click",function() {
        if(flag) return false;
        var mobile = localStorage.userMobile;
        $.ajax({
            type: "post",
            url: domain.test + "/gettelcode",
            data: {
                mobile:mobile
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                    var counter = 90;
                    var timer = setInterval(function() {
                        counter--;
                        if(counter===0) {
                            $(".code").html("获取短息验证码");
                            clearInterval(timer);
                        }else {
                            $(".code").html(counter + "s");
                            flag = true;
                        }
                    },1000);
                }
            }
        });
    });
    //修改支付密码
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".submitBtn").on("click",function() {
        var code = parseInt($(".codeNum").val());
        console.log(code);
        if(!code) {
            tips("请输入验证码");
            return false;
        }
        var logPsd = $(".logPsd").val();
        if(!logPsd) {
            tips("请输入登录密码");
            return false;
        }
        var payPsd = $(".newPsd").val();
        if(!payPsd) {
            tips("请输入新的支付密码");
            return false;
        }
        if(payPsd.length<6||payPsd.length>6) {
            tips("支付密码由6位数字组成");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/resetpaypsd",
            data: {
                password:logPsd,
                payPassword:payPsd,
                captcha:1234
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                    tips("修改成功");
                    setTimeout(function(){
                        history.back();
                    },1000);
                }
                if(response.data.rtnCode===400) {
                    tips(response.data.msg);
                }
                if(response.data.rtnCode===401) {
                    var outTimeCode = response.data.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
});